layui.use(['table', 'admin', 'ax'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;

    /**
     * 收货单位表管理
     */
    var BasicPutgoods = {
        tableId: "basicPutgoodsTable"
    };

    /**
     * 初始化表格的列
     */
    BasicPutgoods.initColumn = function () {
        return [[
            {type: 'checkbox', fixed: 'left'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'putgoodsCustomerId',hide: true, sort: true, title: '客户id', fixed: 'left'},
            {field: 'putgoodsOperateName', sort: true, title: '客户名称', fixed: 'left'},
            {field: 'putgoodsName', sort: true, title: '收货单位名称', fixed: 'left'},
            {field: 'putgoodsContacts', sort: true, title: '联系人'},
            {field: 'putgoodsTel', sort: true, title: '电话'},
            {field: 'putgoodsEmall', sort: true, title: '邮箱'},
            {field: 'putgoodsWebsite', sort: true, title: '网址'},
            {field: 'putgoodsRange', sort: true, title: '经营范围'},
            {field: 'putgoodsIndustry', sort: true, title: '行业类型'},
            {field: 'putgoodsProvinces', sort: true, title: '所属省份'},
            {field: 'putgoodsCiywhere', sort: true, title: '所在城市'},
            {field: 'putgoodsBank', sort: true, title: '开户行'},
            {field: 'putgoodsBankaccount', sort: true, title: '银行账号'},
            {field: 'putgoodsCreditcode', sort: true, title: '社会统一信用代码'},
            {field: 'putgoodsAddress', sort: true, title: '地址'},
            {field: 'putgoodsNotes', sort: true, title: '备注'},
            {field: 'version', hide: true, sort: true, title: '乐观锁'},
            {field: 'createTime', hide: true, sort: true, title: '创建时间'},
            {field: 'updateTime', hide: true, sort: true, title: '修改时间'},
            {field: 'createUser', hide: true, sort: true, title: '创建用户'},
            {field: 'updateUser', hide: true, sort: true, title: '修改用户'},
            {fixed: 'right',width: 80, align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    BasicPutgoods.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        table.reload(BasicPutgoods.tableId, {
            where: queryData, page: {curr: 1},
            done:function () {
                // 搜索按钮点击事件
                $('#btnSearch').click(function () {
                    BasicPutgoods.search();
                });
                // 添加按钮点击事件
                $('#btnAdd').click(function () {
                    BasicPutgoods.openAddDlg();
                });
                // 导出excel
                $('#btnExp').click(function () {
                    BasicPutgoods.exportExcel();
                });
            }
        });
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + BasicPutgoods.tableId,
        url: Feng.ctxPath + '/outOrder/ChoosePutGoods?+goodsCustomerId='+$("#outCustomerId").val(),
        page: true,
        limit: 20,
        height: "full-60",
        toolbar: '#toolbarfrom',
        cellMinWidth: 100,
        cols: BasicPutgoods.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        BasicPutgoods.search();
    });

    function confirm(data){
        parent.OutOrderInfoDlg.data.receivingGoodsCompany = data.putgoodsName;//收货单位名称
        parent.OutOrderInfoDlg.data.contactor = data.putgoodsContacts;//联系人
        parent.OutOrderInfoDlg.data.contactTel = data.putgoodsTel;//联系电话
        parent.OutOrderInfoDlg.data.detailedAddress = data.putgoodsAddress;//地址
        var Index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(Index);
    }

    // 双击行事件
    table.on('rowDouble(' + BasicPutgoods.tableId + ')', function (obj) {
        var data = obj.data;
        confirm(data);
    });

    // 工具条点击事件
    table.on('tool(' + BasicPutgoods.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        if (layEvent === 'confirm') {
            confirm(data);
        }
    });

});
